package cn.appinfo.controller.backend;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appinfo.pojo.AppCategory;
import cn.appinfo.pojo.AppInfo;
import cn.appinfo.pojo.AppVersion;
import cn.appinfo.pojo.DataDictionary;
import cn.appinfo.pojo.Paging;
import cn.appinfo.service.appCategory.AppCategoryService;
import cn.appinfo.service.appVersion.AppVersionService;
import cn.appinfo.service.appinfo.AppInfoService;
import cn.appinfo.service.dataDictionary.DataDictionaryService;

@Controller
@RequestMapping("/manager/backend/app")
public class AppInfoController {
	
	@Resource
	private AppInfoService appInfoService;
	@Resource
	private DataDictionaryService dataDictionaryService;
	@Resource
	private AppCategoryService appCategoryService;
	@Resource
	private AppVersionService appVersionService;
	
	@RequestMapping("/list")
	public String gatAppInfoList(Model model,
			@RequestParam(value="SoftwareName",required=false)String SoftwareName,
			@RequestParam(value="FlatformId",required=false)String FlatformId,
			@RequestParam(value="CategoryLevel1",required=false)String CategoryLevel1,
			@RequestParam(value="CategoryLevel2",required=false)String CategoryLevel2,
			@RequestParam(value="CategoryLevel3",required=false)String CategoryLevel3,
			@RequestParam(value="pageIndex",required=false)String currentPageNo){
		List<AppCategory> categoryLevel1List = getCategoryLevel(0);
		List<AppCategory> categoryLevel2List = null;
		List<AppCategory> categoryLevel3List = null;
		int _FlatformId =0;
		if(FlatformId != null && FlatformId != ""){
			_FlatformId = Integer.parseInt(FlatformId);
		}
		int _categoryLevel1=0;
		if(CategoryLevel1 != null && CategoryLevel1 != ""){
			_categoryLevel1 = Integer.parseInt(CategoryLevel1);
			categoryLevel2List = getCategoryLevel(Integer.parseInt(CategoryLevel1));
		}
		int _categoryLevel2=0;
		if(CategoryLevel2 != null && CategoryLevel2 != ""){
			_categoryLevel2 = Integer.parseInt(CategoryLevel2);
			categoryLevel3List = getCategoryLevel(Integer.parseInt(CategoryLevel2));
		}
		int _categoryLevel3=0;
		if(CategoryLevel3 != null && CategoryLevel3 != ""){
			_categoryLevel3 = Integer.parseInt(CategoryLevel3);
		}
		Paging page = new Paging();
		page.setPageSize(5);
		page.setTotalCount(appInfoService.appInfoCount(SoftwareName,_FlatformId,_categoryLevel1,_categoryLevel2,_categoryLevel3));
		if(currentPageNo == null){
			page.setCurrentPageNo(1);
		}else{
			page.setCurrentPageNo(Integer.parseInt(currentPageNo));
		}
		List<AppInfo> appinfo = appInfoService.getAppInfoList((page.getCurrentPageNo()-1)*page.getPageSize(),page.getPageSize()
															,SoftwareName,_FlatformId,_categoryLevel1,_categoryLevel2,_categoryLevel3);
		List<DataDictionary> dataDictionary = dataDictionaryService.getFlatFormList();
		model.addAttribute("appInfoList",appinfo);
		model.addAttribute("pages", page);
		model.addAttribute("flatFormList", dataDictionary);
		model.addAttribute("categoryLevel1List",categoryLevel1List);
		model.addAttribute("categoryLevel2List",categoryLevel2List);
		model.addAttribute("categoryLevel3List",categoryLevel3List);
		model.addAttribute("queryFlatformId", _FlatformId);
		model.addAttribute("querySoftwareName",SoftwareName);
		model.addAttribute("queryCategoryLevel1", _categoryLevel1);
		model.addAttribute("queryCategoryLevel2", _categoryLevel2);
		model.addAttribute("queryCategoryLevel3", _categoryLevel3);
		return "backend/applist";
	}
	
	private List<AppCategory> getCategoryLevel(int id){
		return appCategoryService.getCategoryLevel(id);
	}
	
	@RequestMapping(value="/categorylevellist.json",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public Object categorylevellist(@RequestParam("pid") String id){
		List<AppCategory> appCategoryList = getCategoryLevel(Integer.parseInt(id));
		return JSON.toJSONString(appCategoryList);
	}
	
	@RequestMapping(value="/check")
	public String check(Model model,
			@RequestParam("aid")String aid,
				@RequestParam("vid")String vid){
		AppInfo appInfo = appInfoService.getAppInfo(Integer.parseInt(aid));
		AppVersion appVersion = 
				appVersionService.getAppVersion(Integer.parseInt(vid));
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("appVersion", appVersion);
		return "backend/appcheck";
	}
	
	@RequestMapping(value="/checksave")
	public String checksave(@Param("id")String id,@Param("status")String status){
		appInfoService.checksave(Integer.parseInt(id),status);
		return "redirect:/manager/backend/app/list";
	}
}
